package org.hxyjs.dao.impl;

import org.hxyjs.dao.ClazzDao;
import org.hxyjs.entity.Clazz;
import org.hxyjs.entity.Clazz;
import org.hxyjs.entity.Clazz;
import org.hxyjs.mapper.ClazzMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("clazzDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ClazzDaoImpl implements ClazzDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ClazzMapper clazzMapper;
    public List<Clazz>  getAllClazzs() throws Exception{
        return clazzMapper.selectByExample(null);
    }

    @Override
    public Integer deleteClazzByClno(String clno)  throws Exception{
        return clazzMapper.deleteByPrimaryKey(clno);
    }

    @Override
    public Integer updateClazzByClno(Clazz clazz)  throws Exception{
        return clazzMapper.updateByPrimaryKey(clazz);
    }

    @Override
    public Integer addClazz(Clazz clazz)  throws Exception{
        clazz.setClno(initId());
        return clazzMapper.insertSelective(clazz);
    }
    public String initId(){
        String maxClno = clazzMapper.getMaxClno();
        return GenNumTools.initId("",3,maxClno);
    }
    @Override
    public List<Clazz> getClazzByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)clazzMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return clazzMapper.getClazzByPage(map);
    }
}
